Matlab: Why is '1' + 1 == 50? [migrated]

Posted by phi on Programmers See other posts from Programmers or by phi
Published on 2014-06-04T15:55:18Z Indexed on 2014/06/04 21:41 UTC
Read the original article Hit count: 226

Matlab has weak dynamic typing, which is what causes this weird behaviour. What I do not understand is what exactly happens, as this result really surprises me.

Edit: To clarify, what I'm describing is clearly a result of Matlab storing chars in ASCII-format, which was also mentioned in the comments. I'm more interested in the way Matlab handles its variables, and specifically, how and when it assigns a type/tag to the values.

Thanks.


'1' 

is a 1-by-1 matrix of chars in matlab and

'123' 

is a 1-by-3 matrix of chars.

As expected,

1

returns a 1-by-1 double.


Now if I enter

'1' + 1

I get 50 as a 1-by-1 double, and if I enter

'123' + 1

I get a 1-by-3 double

[ 50 51 52 ]

Furthermore, if I type

'a' + 1

the result is

98

in a 1-by-1 double.

I assume this has to do with how Matlab stores char-variables in ascii form, but how exactly is it handling these? Are the data actually unityped and tagged, or how does it work?

Thanks.

© Programmers or respective owner

Related posts about dynamic-typing

Related posts about type-systems